﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
using Newtonsoft.Json;
using ZF.BLL.Admin;
namespace ZF.WS.Admin
{
    public class Offering : ZF.BLL.Page.MMPage
    {
        public override ModuleType Module
        {
            get { return ModuleType.LF; }
            protected set
            {
                base.Module = value;
            }
        }
        public virtual string ListPage { get { return string.Format("{0}/game/OfferingList.aspx", AppKey.SiteKey.ManageDirectory); } }
        public virtual string ManagePage { get { return string.Format("{0}/game/OfferingManage.aspx", AppKey.SiteKey.ManageDirectory); } }
        public virtual string CTitle { get { return "供品"; } }
    }
    public partial class OfferingManage : Offering
    {
    

        [PageArgument(Key = "id", CanNull = false, BindOperType = PageOperType.Edit)]
        public int  Id { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                if (this.OperType == PageOperType.AddNew)
                    Add();
                else if (this.OperType == PageOperType.Edit)
                    Edit();
            }
        }
        public void InitControls()
        {

            this.ddlType.DataSource = DictionaryConfig.GOfferingType;
            this.ddlType.DataBind();
      ;
        }

        public void Add()
        {
            InitControls();

        }
        public void AddSubmit(object sender, EventArgs e)
        {


            using (var dc = SiteInfo.CreateDBDataContext())
            {


                var offering = new ZF.Model.G_Offering()
                {
                    Name = this.tbName.Text,
                    Group = this.ddlType.SelectedValue,
                    Memoy = this.tbMemoy.Text.EmptyInt(0),
                    Remark = this.tbRemark.Text,
                    Path = this.SWFUpLoad1.UpLoadFileInfos.ConstituteString(";"),
                };


                try
                {
                    dc.G_Offering.InsertOnSubmit(offering);
                    dc.SubmitChanges();
             
                }
                catch (Exception ex)
                {
                    throw WarnFactory.CreateWarn(ex);
                }
                string msgKey = CTitle;

                SiteInfo.InsertLog("添加" + msgKey, null);
                WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回上一页", msgKey));
                link.AddWarnMessageLink(string.Format("继续添加{0}", msgKey), Request.Url.ToString());
                WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();

            }

        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var offering = dc.G_Offering.FirstOrDefault(p => p.OfferingId == this.Id);
                if (offering == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                InitControls();
                this.tbName.Text = offering.Name;
                this.tbMemoy.Text = string.Concat(offering.Memoy);
                this.tbPhoto.Text = offering.Path;
            
                this.lvImgList.DataSource = offering.Path.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(p => new
                    {
                        File = p
                    });
                this.lvImgList.DataBind();
                this.tbRemark.Text = offering.Remark;
            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var offering = dc.G_Offering.FirstOrDefault(p => p.OfferingId == this.Id);
                    if (offering == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);

                    offering.Name = this.tbName.Text;
                    offering.Memoy = this.tbMemoy.Text.EmptyInt(0);
                    var photoList = this.tbPhoto.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    photoList.AddRange(this.SWFUpLoad1.UpLoadFileInfos);
                    offering.Path = photoList.ConstituteString(";");
                 
                    offering.Remark = this.tbRemark.Text;
                    try
                    {
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = CTitle;

                    SiteInfo.InsertLog("修改" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功修改一个{0}，点击确定按钮返回{0}列表", msgKey));

                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey), Request.Url.ToString());
                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();

                }


            }

        }

    
    }
 
}
